package org.jeecg.modules.workflow.controller.definition;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.workflow.vo.definition.form.*;
import org.jeecg.modules.workflow.controller.BaseController;
import org.jeecg.modules.workflow.convert.definition.BpmFormConvert;
import org.jeecg.modules.workflow.entity.BpmForm;
import org.jeecg.modules.workflow.service.definition.BpmFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "管理后台 - 动态表单")
@RestController
@RequestMapping("/bpm/form")
@Validated
public class BpmFormController extends BaseController {

    @Resource
    private BpmFormService formService;

    @Autowired
    private BpmFormConvert bpmFormConvert;

    @PostMapping("/create")
    @ApiOperation("创建动态表单")
    //@RequiresPermissions("bpm:form:create")
    public Result<Long> createForm(@Valid @RequestBody BpmFormCreateReqVO createReqVO) {
        return Result.OK(formService.createForm(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新动态表单")
    //@RequiresPermissions("bpm:form:update")
    public Result<Boolean> updateForm(@Valid @RequestBody BpmFormUpdateReqVO updateReqVO) {
        formService.updateForm(updateReqVO);
        return Result.OK(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除动态表单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    //@RequiresPermissions("bpm:form:delete")
    public Result<Boolean> deleteForm(@RequestParam("id") String id) {
        formService.deleteForm(id);
        return Result.OK(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得动态表单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    //@RequiresPermissions("bpm:form:query")
    public Result<BpmFormRespVO> getForm(@RequestParam("id") String id) {
        BpmForm form = formService.getForm(id);
        return Result.OK(bpmFormConvert.convert(form));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获得动态表单的精简列表", notes = "用于表单下拉框")
    public Result<List<BpmFormSimpleRespVO>> getSimpleForms() {
        List<BpmForm> list = formService.getFormList();
        return Result.OK(bpmFormConvert.convertList2(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得动态表单分页")
    //@RequiresPermissions("bpm:form:query")
    public Result<IPage<BpmFormRespVO>> getFormPage(@Valid BpmFormPageReqVO pageVO) {
        IPage<BpmForm> pageList = formService.getFormPage(pageVO);
        IPage<BpmFormRespVO> pageResult = bpmFormConvert.convertPage(pageList);
        return Result.OK(pageResult);
    }

}
